﻿/// <reference path="Turbo_common.js" />
/// <reference path="AjaxResponse.js" />
/// <reference path="PostScenario.js" />

TA.Turbo.Common._DoPostBack_Ajax = function _DoPostBack_Ajax(eventTarget, eventName, eventArgument, scenario, callBackFunction)
{
	if (_____a)
		scenario = new $PostScenario();

	for (var iProgressControl = 0; iProgressControl < scenario.ProgressControls.length; ++iProgressControl)
	{
		scenario.ProgressControls[iProgressControl].Start();
	}

	document.cookie = "JS=1; path=/";
	document.cookie = "NOJS=1; expires=Tue, 01 Feb 2000 05:00:00 GMT; path=/";

	var params = new Object();
	params["__VIEWSTATE"] = $("__VIEWSTATE").value;

	for (var iPostControl = 0; iPostControl < scenario.PostControls.length; ++iPostControl)
	{
		var pc = scenario.PostControls[iPostControl];
		if (_____a)
			pc = new $PostControl();

		var sendAs = TA.Turbo.Common.GetControlServerName(pc.get_Control());

		if (pc.SendAs != null && pc.SendAs != "")
			sendAs = pc.SendAs;

		if (pc.Value)
			params[sendAs] = pc.get_Control().get_Object().get_SerializedValue();

		if (pc.Properties !== undefined)
		{
			if (pc.Properties.length > 0)
			{
				params[sendAs + TA.Turbo.Common.StateFieldUniqueIdPostfix] = TA.Turbo.Common._GetPropertiesJson(pc);
			}
		}
	}

	if (!scenario.Clean)
	{
		params["__T"] = eventTarget;
		params["__N"] = eventName;
		if (eventArgument != null)
		{
			params["__A"] = eventArgument;
		}
	}

	var postUrl = scenario.PostUrl;
	if (scenario.SendHashString)
	{
		if (location.hash != null && location.hash != "")
			postUrl = Ajax.AppendToUrl(postUrl, location.hash.substr(1));
	}

	if (TA.Turbo.Common._SaveCookies != null)
		TA.Turbo.Common._SaveCookies();

	if (scenario.HttpVerb == HttpVerb.Post)
		Ajax.Post(postUrl, params, TA.Turbo.Common._DoPostBack_Ajax_Response.bind(null, callBackFunction), scenario);
	else
		Ajax.Get(postUrl, params, TA.Turbo.Common._DoPostBack_Ajax_Response.bind(null, callBackFunction), scenario);
}
TA.Turbo.Common._DoPostBack_Ajax_Response = function _DoPostBack_Ajax_Response(callBackFunction, responseText, request, scenario)
{
	if (_____a)
		scenario = new $PostScenario();

	for (var iProgressControl = 0; iProgressControl < scenario.ProgressControls.length; ++iProgressControl)
	{
		scenario.ProgressControls[iProgressControl].Stop();
	}
	switch (request.status)
	{
		case 200:
			if (responseText.indexOf("#####302") != -1)
			{
				location.href = responseText.substr(responseText.indexOf("#####302") + 8);
				return;
			}

			var responseParts = responseText.split("<!>");
			var __JE = new Array(); 
			__JE.push.apply(__JE, responseParts);
			__JE.splice(0, 1);
			var json = eval("(" + responseParts[0] + ")");

			var response = new $AjaxResponse();
			response.Deserialize(json);
			response.Process(scenario);

			if (typeof (callBackFunction) == "function")
				callBackFunction(response);

			if (TA.Turbo.Common._LoadCookies != null)
				TA.Turbo.Common._LoadCookies();
			break;
		default:
			alert("AJAX response status: " + request.status);
			var d = document.createElement("div");
			d.innerHTML = responseText;
			document.body.appendChild(d);
			scenario.RaiseEvent("Error", response);
			break;

	}
}

